﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using static AutoEditUI.Code128;

namespace AutoEditUI.UserControls
{
    internal class UPC
    {
        public UPC()
        {
        }

        internal ImageSource GetUPCImageMK(string p_Text, double w, double h)
        {
         //   //Width = BarWidth * Encoded_Value.Length ?? Width;

         //   //// Automatically calculate Height if applicable.
         //   //Height = (int?)(Width / AspectRatio) ?? Height;

         // //  int ILHeight = Height;
         //   int topLabelAdjustment = 0;

         //   int shiftAdjustment = 0;

         //   // int iBarWidth = Width / Encoded_Value.Length;

         //   ////set alignment
         //   //switch (Alignment)
         //   //{
         //   //    case AlignmentPositions.LEFT:
         //   //        shiftAdjustment = 0;
         //   //        break;
         //   //    case AlignmentPositions.RIGHT:
         //   //        shiftAdjustment = (Width % Encoded_Value.Length);
         //   //        break;
         //   //    case AlignmentPositions.CENTER:
         //   //    default:
         //   //        shiftAdjustment = (Width % Encoded_Value.Length) / 2;
         //   //        break;
         //   //}//switch
         //   bool IncludeLabel = true;
         //   if (IncludeLabel)
         //   {
         //      // if ((AlternateLabel == null || RawData.StartsWith(AlternateLabel)) && _StandardizeLabel)
         //       {
         //           // UPCA standardized label
         //           //  string defTxt = RawData;

         //           string defTxt = p_Text;
         //           string labTxt = defTxt.Substring(0, 1) + "--" + defTxt.Substring(1, 6) + "--" + defTxt.Substring(7);

         //           //Font labFont = new Font(this.LabelFont != null ? this.LabelFont.FontFamily.Name : "Arial", Labels.getFontsize(Width, Height, labTxt), FontStyle.Regular);
         //           //if (this.LabelFont != null)
         //           //{
         //           //    this.LabelFont.Dispose();
         //           //}
         //           //LabelFont = labFont;

         //           //ILHeight -= (labFont.Height / 2);

         //           //iBarWidth = (int)Width / Encoded_Value.Length;
         //       }
         //       //else
         //       //{
         //       //    // Shift drawing down if top label.
         //       //    if ((LabelPosition & (LabelPositions.TOPCENTER | LabelPositions.TOPLEFT | LabelPositions.TOPRIGHT)) > 0)
         //       //        topLabelAdjustment = this.LabelFont.Height;

         //       //    ILHeight -= this.LabelFont.Height;
         //       //}
         //   }

         //  // bitmap = new Bitmap(Width, Height);
         //   //int iBarWidthModifier = 1;
         //   //if (iBarWidth <= 0)
         //   //    throw new Exception("EGENERATE_IMAGE-2: Image size specified not large enough to draw image. (Bar size determined to be less than 1 pixel)");

         //   //draw image
         //   int pos = 0;
         //   int halfBarWidth = (int)(w * 0.5);

         ////   using (Graphics g = Graphics.FromImage(bitmap))
         //   {
         //       //clears the image and colors the entire background
         //    //   g.Clear(BackColor);

         //       //lines are fBarWidth wide so draw the appropriate color line vertically
         //       //using (Pen backpen = new Pen(BackColor, iBarWidth / iBarWidthModifier))
         //       //{
         //       //    using (Pen pen = new Pen(ForeColor, iBarWidth / iBarWidthModifier))
         //       //    {
         //               while (pos < Encoded_Value.Length)
         //               {
         //                   if (Encoded_Value[pos] == '1')
         //                   {
         //                       g.DrawLine(pen, new Point(pos * iBarWidth + shiftAdjustment + halfBarWidth, topLabelAdjustment), new Point(pos * iBarWidth + shiftAdjustment + halfBarWidth, ILHeight + topLabelAdjustment));
         //                   }

         //                   pos++;
         //               }//while
         //       //    }//using
         //       //}//using
         //   }//using
         //   //if (IncludeLabel)
         //   //{
         //   //    if ((AlternateLabel == null || RawData.StartsWith(AlternateLabel)) && _StandardizeLabel)
         //   //    {
         //   //        Labels.Label_UPCA(this, bitmap);
         //   //    }
         //   //    else
         //   //    {
         //   //        Labels.Label_Generic(this, bitmap);
         //   //    }
         //   //}
            var _Text = p_Text;
            System.Windows.Media.DrawingImage _CodeImage = GetGraphic1(_Text, new System.Windows.Size(200, 40), System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.White);
            // GetViewText(_CodeImage, _ViewText);
            return _CodeImage;
        }

        private DrawingImage GetGraphic1(string p_Text, System.Windows.Size viewBox, System.Windows.Media.Brush darkBrush, System.Windows.Media.Brush lightBrush, bool drawQuietZones = true)
        {
            System.Windows.Media.DrawingGroup drawing = new System.Windows.Media.DrawingGroup();
            drawing.Children.Add(new System.Windows.Media.GeometryDrawing(lightBrush, null, new RectangleGeometry(new System.Windows.Rect(new System.Windows.Point(0, 0), viewBox))));
            var group = new GeometryGroup();
            // double x = 0d, y = 0d;

            char[] _Value = p_Text.ToCharArray();
            int _Width = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                _Width += Int32.Parse(_Value[i].ToString()) * (1);
            }
            // System.Drawing.Bitmap _CodeImage = new System.Drawing.Bitmap(_Width, (int)m_Height);
            //  System.Drawing.Graphics _Garphics = System.Drawing.Graphics.FromImage(_CodeImage);
            //Pen _Pen;
            int _LenEx = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (1);  //获取宽和放大系数
                if (!((i & 1) == 0))
                {
                    // group.Children.Add(new RectangleGeometry(new Rect(x * 10, y, 3, 10)));
                    //_Pen = new Pen(Brushes.White, _ValueNumb);
                    //  _Garphics.FillRectangle(System.Drawing.Brushes.White, new System.Drawing.Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
                }
                else
                {
                    group.Children.Add(new RectangleGeometry(new System.Windows.Rect(_LenEx, 0, _ValueNumb, (int)viewBox.Height)));
                    //_Pen = new Pen(Brushes.Black, _ValueNumb);
                    //_Garphics.FillRectangle(System.Drawing.Brushes.Black, new System.Drawing.Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
                }
                //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
                _LenEx += _ValueNumb;
            }
            Label_UPCA(this, group);
            drawing.Children.Add(new GeometryDrawing(darkBrush, null, group));

            return new DrawingImage(drawing);
        }

        private void Label_UPCA(UPC uPC, GeometryGroup group)
        {
            //try
            //{
            //    int iBarWidth = (int)(uPC.Width / uPC.EncodedValue.Length);
            //    int halfBarWidth = (int)(iBarWidth * 0.5);
            //    string defTxt = uPC.RawData;

            //    using (FontFamily labFont = new FontFamily("Arial", getFontsize((int)((Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length) * 0.9f), img.Height, defTxt), FontStyle.Regular))
            //    {
            //        int shiftAdjustment;
            //        //switch (Barcode.Alignment)
            //        //{
            //        //    case AlignmentPositions.LEFT:
            //        //        shiftAdjustment = 0;
            //        //        break;
            //        //    case AlignmentPositions.RIGHT:
            //        //        shiftAdjustment = (Barcode.Width % Barcode.EncodedValue.Length);
            //        //        break;
            //        //    case AlignmentPositions.CENTER:
            //        //    default:
            //        //        shiftAdjustment = (Barcode.Width % Barcode.EncodedValue.Length) / 2;
            //        //        break;
            //        //}//switch

            //      //  using (Graphics g = Graphics.FromImage(img))
            //        {
            //            //g.DrawImage(img, (float)0, (float)0);

            //            //g.SmoothingMode = SmoothingMode.HighQuality;
            //            //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //            //g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //            //g.CompositingQuality = CompositingQuality.HighQuality;
            //            //g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            //            //StringFormat f = new StringFormat();
            //            //f.Alignment = StringAlignment.Near;
            //            //f.LineAlignment = StringAlignment.Near;
            //            //int LabelY = 0;

            //            ////Default alignment for UPCA
            //            //LabelY = img.Height - labFont.Height;
            //            //f.Alignment = StringAlignment.Near;

            //            //float w1 = iBarWidth * 4; //Width of first block
            //            //float w2 = iBarWidth * 34; //Width of second block
            //            //float w3 = iBarWidth * 34; //Width of third block

            //            //float s1 = shiftAdjustment - iBarWidth;
            //            //float s2 = s1 + (iBarWidth * 12); //Start position of block 2
            //            //float s3 = s2 + w2 + (iBarWidth * 5); //Start position of block 3
            //            //float s4 = s3 + w3 + (iBarWidth * 8) - halfBarWidth;

            //            ////Draw the background rectangles for each block
            //            //using (SolidBrush backBrush = new SolidBrush(Barcode.BackColor))
            //            //{
            //            //    g.FillRectangle(backBrush, new RectangleF(s2, (float)LabelY, w2, (float)labFont.Height));
            //            //    g.FillRectangle(backBrush, new RectangleF(s3, (float)LabelY, w3, (float)labFont.Height));
            //            //}

            //            //draw data string under the barcode image
            //          //  using (SolidBrush foreBrush = new SolidBrush(Barcode.ForeColor))
            //            {
            //            //    using (Font smallFont = new Font(labFont.FontFamily, labFont.SizeInPoints * 0.5f, labFont.Style))
            //                {
            //                    g.DrawString(defTxt.Substring(0, 1), smallFont, foreBrush, new RectangleF(s1, (float)img.Height - smallFont.Height, (float)img.Width, (float)labFont.Height), f);
            //                    g.DrawString(defTxt.Substring(1, 5), labFont, foreBrush, new RectangleF(s2 - iBarWidth, (float)LabelY, (float)img.Width, (float)labFont.Height), f);
            //                    g.DrawString(defTxt.Substring(6, 5), labFont, foreBrush, new RectangleF(s3 - iBarWidth, (float)LabelY, (float)img.Width, (float)labFont.Height), f);
            //                    g.DrawString(defTxt.Substring(11), smallFont, foreBrush, new RectangleF(s4, (float)img.Height - smallFont.Height, (float)img.Width, (float)labFont.Height), f);
            //                }
            //            }

            //            g.Save();
            //        }
            //    }//using
            //    return img;
            //}//try
            //catch (Exception ex)
            //{
            //    throw new Exception("ELABEL_UPCA-1: " + ex.Message);
            //}//catch
        }
    }
}